// https://www.acwing.com/problem/content/150/

#include <iostream>
#include <vector>
#include <queue>
using namespace std;

int main() {
    int n, t;
    scanf("%d\n", &n);
    priority_queue<int, vector<int>, greater<int>> PQ;
    for (int i = 0; i < n; i++) {
        scanf("%d", &t);
        PQ.push(t);
    }

    long long ans = 0;
    while (PQ.size() > 1) {
        int top1 = PQ.top();
        PQ.pop();
        int top2 = PQ.top();
        PQ.pop();
        int w = top1 + top2;
        ans += w;
        PQ.push(w);
    }

    printf("%lld\n", ans);
    return 0;
}